#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL n;
string s;
map<string,LL> mp;

void CalcS(string s,LL id) {
	LL l=s.size();
	string t="";
	if (7>=l) {
		cout<<"ERR"<<endl;
		return;
	}
	LL cd=1,shu=0;
	for (LL i=7;i<l;) {
		if (s[i]>='0' && s[i]<='9') {
			LL w=0;
			if (s[i]=='0' && i!=l-1 && (s[i+1]>='0' && s[i+1]<='9')) {
				cout<<"ERR"<<endl;
				return;
			}
			while (i<l && s[i]>='0' && s[i]<='9') {
				w=w*10+s[i]-'0';
				i++;
			}
			if (cd<=4 && w>255) {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd==5 && w>65535) {
				cout<<"ERR"<<endl;
				return;
			}
			shu++;
		}
		else {
			if (cd<=3 && s[i]!='.') {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd==4 && s[i]!=':') {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd>4) {
				cout<<"ERR"<<endl;
				return;
			}
			cd++;
			i++;
		}
	}
	
	if (cd!=5 || shu!=5) {
		cout<<"ERR"<<endl;
		return;
	}

	for (LL i=7;i<l;i++) t+=s[i];
	
	if (mp[t]) {
		cout<<"FAIL"<<endl;
		return;
	}
	cout<<"OK"<<endl;
	mp[t]=id;
	return;
}

void CalcC(string s) {
	LL l=s.size();
	string t="";
	if (7>=l) {
		cout<<"ERR"<<endl;
		return;
	}
	LL cd=1,shu=0;
	for (LL i=7;i<l;) {
		if (s[i]>='0' && s[i]<='9') {
			LL w=0;
			if (s[i]=='0' && i!=l-1 && (s[i+1]>='0' && s[i+1]<='9')) {
				cout<<"ERR"<<endl;
				return;
			}
			while (i<l && s[i]>='0' && s[i]<='9') {
				w=w*10+s[i]-'0';
				i++;
			}
			if (cd<=4 && w>255) {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd==5 && w>65535) {
				cout<<"ERR"<<endl;
				return;
			}
			shu++;
		}
		else {
			if (cd<=3 && s[i]!='.') {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd==4 && s[i]!=':') {
				cout<<"ERR"<<endl;
				return;
			}
			if (cd>4) {
				cout<<"ERR"<<endl;
				return;
			}
			cd++;
			i++;
		}
	}

	if (cd!=5 || shu!=5) {
		cout<<"ERR"<<endl;
		return;
	}

	for (LL i=7;i<l;i++) t+=s[i];
		
	if (!mp[t]) {
		cout<<"FAIL"<<endl;
		return;
	}
	cout<<mp[t]<<endl;
	return;
}

int main() {
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	
	cin>>n;
	getline(cin,s);
	for (LL i=1;i<=n;i++) {
		getline(cin,s);
		if (s[0]=='S') CalcS(s,i);
		else CalcC(s);
	}
	
	return 0;
}
